
$(function(){
	
	var arr = document.cookie.match(new RegExp("(^| )"+"username"+"=([^;]*)(;|$)"));
	if(arr !=null){
		var username = unescape(arr[2]);//拿到cookie
		$('#username').text(username);
		ajaxSubmit(username);
		}
	
});

$(document).on('click',"#edit",function(){
	 $('#editalert').css('display','none');
	 $('#erroralert').css('display','none');
	 $('#editfail').css('display','none');	
	 $('#edisuccess').css('display','none');	
	if($('#editname').val().trim()==""){
		 $('#editalert').css('display','none');
		 $('#editfail').css('display','none');	
		 $('#edisuccess').css('display','none');
		$('#erroralert').css('display','block');
		var n = $('#editname').parent().parent().siblings('label').text()
		$('#msgerror').text(n);
	}
	else if($('#editage').val()<0||$('#editage').val()>100){
		 $('#editalert').css('display','none');
		 $('#editfail').css('display','none');	
		 $('#edisuccess').css('display','none');
		$('#erroralert').css('display','block');
		var n = $('#editage').parent().parent().siblings('label').text()
		$('#msgerror').text(n);
	}
	else if(isEmail($('#editemail').val())==false){
		 $('#editalert').css('display','none');
		 $('#editfail').css('display','none');	
		 $('#edisuccess').css('display','none');
		$('#erroralert').css('display','block');
		var n = $('#editemail').parent().parent().siblings('label').text()
		$('#msgerror').text(n);
	}
	
	else{
		 $('#editalert').css('display','none');
		 $('#erroralert').css('display','none');
		 $('#editfail').css('display','none');	
		 $('#edisuccess').css('display','none');
		 $('#myModal').modal({
				keyboard: true
			})
	}

});


$(document).on('click',"#editconfirm",function(){
	 var name = $('#editname').val();
	 var nickname = $('#editnickname').val();
	 var age = $('#editage').val();
	 var email = $('#editemail').val();
	 var sex =  $('input[name="sex"]:checked').val();
	 var id = $('#id').val();
	 ajaxEditSubmit(name,nickname,age,email,sex,id);
});

var xmlHttpRequest = null;

function ajaxSubmit(name){
//	window.alert(window.ActiveXObject);

	
	if(window.ActiveXObject){	//是IE浏览器

		xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");

	}else if(window.XMLHttpRequest){	//除IE外的其他浏览器

		xmlHttpRequest = new XMLHttpRequest();
	}

	/*
		使用Ajax准备向服务器端发送一个请求
	*/
	if(null != xmlHttpRequest){	//使用GET方式，			//异步
	
	//	xmlHttpRequest.open("GET", "AjaxServlet?num1=" + num1 + "&num2=" + num2, "true");

		xmlHttpRequest.onreadystatechange = ajaxCallBack;	//注册一个回调函数

		//向服务器端发送数据
	//	xmlHttpRequest.send(null);	//get方式设置为null

		//post方式
		xmlHttpRequest.open("POST", "editServlet", "true");
		//使用post方式提交数据，必须加上下面这行代码
		xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		
		xmlHttpRequest.send("name=" + name);

	}
}

function ajaxCallBack(){

	if(xmlHttpRequest.readyState == 4){

		if(xmlHttpRequest.status == 200){
			var responseText = xmlHttpRequest.responseText;	
			
		//	document.getElementById("content").innerText = responseText;	
		// alert(responseText);
		responseText = responseText.trim();
		if(responseText == ""){
			$('#editalert').css('display','block');	
		}
		 else{
			 json = eval('(' + responseText +')');
			 $('#id').val(json['id']);
			 $('#editname').val(json['name']);
			 $('#editnickname').val(json['nickname']);
			 $('#editage').val(json['age']);
			 $('#editemail').val(json['email']);
			 var sex = json['sex'];
			 if(sex=="男"){
				 $('#sexman').attr("checked","true");
			 }else{
				 $('#sexwoman').attr("checked","true");
			 }
			 
		} 
			
		}
	} 
}
//提交编辑
function ajaxEditSubmit(name,nickname,age,email,sex,id){
//	window.alert(window.ActiveXObject);

	
	if(window.ActiveXObject){	//是IE浏览器

		xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");

	}else if(window.XMLHttpRequest){	//除IE外的其他浏览器

		xmlHttpRequest = new XMLHttpRequest();
	}

	/*
		使用Ajax准备向服务器端发送一个请求
	*/
	if(null != xmlHttpRequest){	//使用GET方式，			//异步
	
	//	xmlHttpRequest.open("GET", "AjaxServlet?num1=" + num1 + "&num2=" + num2, "true");

		xmlHttpRequest.onreadystatechange = ajaxEditCallBack;	//注册一个回调函数

		//向服务器端发送数据
	//	xmlHttpRequest.send(null);	//get方式设置为null

		//post方式
		xmlHttpRequest.open("POST", "confirmEditServlet", "true");
		//使用post方式提交数据，必须加上下面这行代码
		xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		
		xmlHttpRequest.send("id="+id+"&name=" + name+"&nickname="+nickname+"&age="+age+"&sex"+sex+"&email="+email);

	}
}

function ajaxEditCallBack(){

	if(xmlHttpRequest.readyState == 4){

		if(xmlHttpRequest.status == 200){
			var responseText = xmlHttpRequest.responseText;	
			
		//	document.getElementById("content").innerText = responseText;	
		// alert(responseText);
		responseText = responseText.trim();
		if(responseText == "fail"){
			 $('#edisuccess').css('display','none');
			 $('#editalert').css('display','none');
			 $('#erroralert').css('display','none');
			$('#editfail').css('display','block');	
		}
		 else{
			 
			 $('#editalert').css('display','none');
			 $('#erroralert').css('display','none');
			 $('#editfail').css('display','none');	
			 $('#edisuccess').css('display','block');	
			 
			 
		} 
			
		}
	} 
}
function isEmail(strEmail) {
	if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
	return true;
	else
	return false;
	}